<?php
/*
 * With bindParam, you can only pass variables ; not values
* with bindValue, you can pass both (values, obviously, and variables)
 * Statement é uma interface utilizada para executar instruções SQL. 
 * 
Nesta interface, existem vários métodos para tratar instruções S
 */
try{
$con = new PDO("mysql:host=localhost;port=3306;dbname=teste","root","");
$con-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$con-> exec("set names utf8");	

echo "==> SELECT 1<br/>";

$sql = "SELECT nome, cpf FROM aluno";
$stm = $con->prepare($sql);
$stm-> execute();

while($row = $stm->fetch(PDO::FETCH_OBJ)) {
    echo (
         
         "Nome: ".$row->nome.'<br />'.
         "CPF: ".$row->cpf.'<br />'
            
            );
}


echo "==> SELECT 2<br/>";    		

$sql = "SELECT nome, cpf FROM aluno WHERE id=? ";
$stm = $con->prepare($sql);
$stm->bindValue(1, "1");
$stm-> execute();

while($row = $stm->fetch(PDO::FETCH_OBJ)) {
    echo ($row->cpf.'<br />');
 }
         
$sql = "INSERT INTO aluno(nome,cpf,idade) VALUES(?, ?, ?)";
$stmt = $con->prepare($sql);
$stmt->bindValue(1,'Joao');
$stmt->bindValue(2,'123');
$stmt->bindValue(3,'40');
$stmt->execute();


$sql = "DELETE FROM aluno WHERE id=? ";
$stmt = $con->prepare($sql);
$stmt->bindValue(1,'1');
$stmt->execute();


$sql = "UPDATE aluno SET nome=?,cpf=? WHERE cpf=? ";
$stmt = $con->prepare($sql);
$stmt->bindValue(1,'Tesse');
$stmt->bindValue(2,'66666');
$stmt->bindValue(3,'123');
$stmt->execute();
}
catch(PDOException $e){
    $e->getMessage();
}
catch (Exception $e){
    echo $e->getMessage();
		}


?>
